package com.dkd.day20250616;

import java.util.ArrayList;
import java.util.List;

public class SolutionpreorderTraversal {
    public static void main(String[] args) {

    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类
     * @return int整型一维数组
     */
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list = new ArrayList<Integer>();
        preorder(list, root);
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }

    public List<Integer> preorder (List<Integer> list ,TreeNode root) {
        // write code here
        if(root == null){
            return list;
        }
        list.add(root.val);
        preorder(list,root.left);
        preorder(list,root.right);
        return list;
    }
}
